#include "color_button.h"

#include <QColorDialog>
#include <QPixmap>

ColorButton::ColorButton(QWidget *parent) : QToolButton(parent), color_(Qt::white) {
  setColor(color_);
  connect(this, SIGNAL(clicked()), this, SLOT(chooseColor()));
}

QColor ColorButton::color() const {
  return color_;
}

void ColorButton::setColor(const QColor& color) {
  color_ = color;
  QPixmap swatch(16, 12);
  swatch.fill(color_);
  setIcon(QIcon(swatch));
  emit colorChanged(color);
}

void ColorButton::chooseColor() {
  QColor color = QColorDialog::getColor(color_, parentWidget());
  if (color.isValid()) {
    setColor(color);
  }
}
